package com.songcan.oauth.client;

import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class ClientService extends JdbcClientDetailsService {

    @Autowired
    private RedisTemplate redisTemplate;

    public ClientService(DataSource dataSource){
        super(dataSource);
    }


    /**
     * 重写原生方法支持redis缓存
     *
     * @param clientId
     * @return
     */
    @SneakyThrows
    @Override
    public ClientDetails loadClientByClientId(String clientId) throws InvalidClientException {
//          String key = "CLIENTDETAILS:"+clientId;
//          ClientDetails clientDetails = (ClientDetails) redisTemplate.opsForValue().get(key);
//
//          if(Objects.isNull(clientDetails)){
//              clientDetails =  super.loadClientByClientId(clientId);
//              if(Objects.nonNull(clientDetails)) {
//                  redisTemplate.opsForValue().set(key,clientDetails,3, TimeUnit.HOURS);
//              }
//          }
//          return  clientDetails;
        return  super.loadClientByClientId(clientId);
    }
}
